package com.huyi.zxing;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.adapter.JieSuanRenAdapter;
import com.adapter.QueryAdapter;
import com.bean.QueryBean;
import com.example.twodimensioncodedemo.QueryTicket;
import com.google.gson.Gson;
import com.utils.JudgeIntenet;
import com.utils.SharedPreferencesUtils;
import com.utils.UrlUtlis;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.List;

import demo.zdc.com.myapplication.R;
import okhttp3.Call;

public class BarCodeActivity  extends Activity implements  OnClickListener{
	private ImageView btn_scan;
	private TextView resultTextView;
	private EditText et_name;
	private Spinner sp_jiesuanren;
	private ArrayAdapter<String> adapter;
	private static final String[] m={"张三","李四","王五","赵六"};
	private static final String[] m1={"展馆1","展馆2","展馆3","展馆4"};
	private Spinner sp_selectedzhanguan;
	private TextView textname,person;
	private TextView btn_query;
	List<QueryBean.ExhibitionListBean> exhibitionList;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.barcode);
		JudgeIntenet judgeIntenet = new JudgeIntenet(getApplicationContext());
		judgeIntenet.initIntener();
//		resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
		btn_scan = (ImageView) findViewById(R.id.btn_scan);
	//	et_name = (EditText) findViewById(R.id.et_Name);

		sp_jiesuanren = (Spinner) findViewById(R.id.sp_jiesuanren);
		sp_selectedzhanguan = (Spinner) findViewById(R.id.sp_selectedzhanguan);

		btn_query = (TextView)findViewById(R.id.btn_query);
		btn_query.setOnClickListener(this);

		/**
		 * 获取查询数据
		 */
		OkHttpUtils.get()
				  .url(UrlUtlis.QUERY)
				  .build()
				  .execute(new StringCallback() {
					  @Override
					  public void onError(Call call, Exception e, int id) {
						  
					  }
					  @Override
					  public void onResponse(String response, int id) {

						  Gson gson = new Gson();
						  QueryBean queryBean = gson.fromJson(response, QueryBean.class);
						  exhibitionList = queryBean.getExhibitionList();

						  QueryAdapter queryAdapter = new QueryAdapter(BarCodeActivity.this, exhibitionList);
						  sp_selectedzhanguan.setAdapter(queryAdapter);


						 //结算人信息
						  final List<QueryBean.UserListBean> userList = queryBean.getUserList();
						  JieSuanRenAdapter jieSuanRenAdapter = new JieSuanRenAdapter(BarCodeActivity.this, userList);
						  sp_jiesuanren.setAdapter(jieSuanRenAdapter);


						  sp_jiesuanren.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
							  @Override
							  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

								//  view.setVisibility(View.INVISIBLE);
								  int no = userList.get(position).getNo();
								  SharedPreferencesUtils.saveInt(BarCodeActivity.this,"userNo",no);
							//	  person.setText(userList.get(position).getUser());
							  }

							  @Override
							  public void onNothingSelected(AdapterView<?> parent) {

							  }
						  });

						  sp_selectedzhanguan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
							  @Override
							  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
								//  view.setVisibility(View.INVISIBLE);
								  SharedPreferencesUtils.saveInt(BarCodeActivity.this,"exhibit",exhibitionList.get(position).getNo());
									//  textname.setText(exhibitionList.get(position).getExhibition());

							  }

							  @Override
							  public void onNothingSelected(AdapterView<?> parent) {

							  }
						  });
					  }
				  });
		
		


		//setPush(m1,sp_selectedzhanguan);
		btn_scan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//打开扫描界面扫描条形码或二维码
				Intent openCameraIntent = new Intent(BarCodeActivity.this,CaptureActivity.class);
				startActivityForResult(openCameraIntent, 0);
			}
		});
	}


	/**
	 * 设置下拉列表
     */

	public void setPush(List<String> list,Spinner sp){
	//	adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arrs);

		//设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		//将adapter 添加到spinner中
		sp.setAdapter(adapter);

		//添加事件Spinner事件监听
		sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(getApplicationContext(),"你选择的是"+m[position],Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});


		//设置默认值
		sp_jiesuanren.setVisibility(View.VISIBLE);

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		//处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			resultTextView.setText(scanResult);

		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()){
			//查询展馆售票情况
			case R.id.btn_query:
				Intent intent = new Intent(BarCodeActivity.this, QueryTicket.class);
				intent.putExtra("exhibitionLists",(Serializable)exhibitionList);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getApplicationContext().startActivity(intent);
				break;
		}
	}

}
